<HTML><HEAD> <!-- -------------------------- Cookie Utilities: Examples -------------------------- --> <SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers /* THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com Copyright (c)1998 by Charles River Media. All Rights Reserved. This applet can only be re-used or modifed by license holders of the JavaScript Cookbook CD-ROM. Credit must be given in the source code and this copyright notice must be maintained. If you do not hold a license to the JavaScript Cookbook, you may NOT duplicate or modify this code for your own use. Use at your own risk. No warranty is given or implied of the suitability of this applet for any specific application. Neither Erica Sadun nor Charles River Media will be held responsible for any unwanted effects due to the use of this applet or any derivative. */ // --------------------COOKIE STRING UTILITIES--------------------- // Substitute character string c2 for every c1 in aString function subst(aString, c1, c2) { if (aString == "") return aString if (c1 == "") return aString // avoid infinite recursion when substituting aa for a by // providing an offset into the string. var argc = subst.arguments.length if (argc < 4) {n = 0} else {n = subst.arguments[3]} // find the first occurence of c1 after the threshold var i = aString.indexOf(c1, n) // stop recursion and return the current string when c1 not found if (i < 0) return aString // extract substrings s1 and s2 around the c1 var s1 = aString.substring(0, i) var s2 = aString.substring(i+c1.length, aString.length) // recurse with this new string return subst(s1+c2+s2, c1, c2, (i+c2.length)) } // Strips a string of blanks on either end function stripBlanks(aString) { var tmp = ""+aString var bottom = 0 var top = tmp.length if (tmp == "") return tmp while (tmp.substring(bottom, bottom + 1) == " ") bottom++ if (bottom >= top) return("") while (tmp.substring(top - 1, top) == " ") top -= 1 return (tmp.substring(bottom, top)) } // Count Occurances of a Substring function count(aString, aSubstring) { // initialize counter and offset var count = 0 var offset = 0 var where = 0 var tmp = ""+aString // search until no more found while ((offset < tmp.length) && ((where = tmp.indexOf(aSubstring, offset)) >= 0)) { count++ offset = where+aSubstring.length } // return it return count } // Encookie -- code spaces, semicolons and commas function encookie(aString) { var tmp = ""+aString tmp = subst(tmp, " ", "%20") tmp = subst(tmp, ",", "%2C") tmp = subst(tmp, ";", "%3B") tmp = subst(tmp, "=", "%3D") return tmp } // Decookie -- decode spaces, semicolons and commas function decookie(aString) { var tmp = ""+aString tmp = subst(tmp, "%20", " ") tmp = subst(tmp, "%2C", ",") tmp = subst(tmp, "%3B", ";") tmp = subst(tmp, "%3D", "=") return tmp } // Search for a substring and chop before it function chopit(aString, aSubstring) { var tmp = ""+aString if (tmp.length == 0) return tmp if (aSubstring.length == 0) return tmp var where if ((where = tmp.indexOf(aSubstring)) < 0) return tmp return (tmp.substring(0, where)) } // Search for a substring and lop off everything after it function lopit(aString, aSubstring) { var tmp = ""+aString if (tmp.length == 0) return tmp if (aSubstring.length == 0) return tmp var where if ((where = tmp.indexOf(aSubstring)) < 0) return tmp return (tmp.substring(where+aSubstring.length, tmp.length)) } //------------------STRING-ARRAY UTILITIES------------------- // Find the substring at index n, counting 0 to n function doIndex(aString, n) { var str=""+aString // Count until the correct index for(var i = 0; i < n; i++) { var where = str.indexOf(':', 1) str = str.substring(where+1, str.length) } // Lop off the end of the string return str.substring(0, str.indexOf(':')) } //------------------CUSTOM GMT UTILITY----------------------------- // Return the 3-letter symbol for month #n function month(n) { var m = "Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec:" return doIndex(m, n) } // Return the 3-letter symbol for day #n function day(n) { var d = "Sun:Mon:Tue:Wed:Thu:Fri:Sat:" return doIndex(d, n) } // A Custom "GMT" Function function myGMT(d) { tmp = day(d.getDay())+', '+d.getDate()+' '+month(d.getMonth()) tmp += ' 19'+d.getYear()+' ' if (d.getHours() < 10) tmp += '0' tmp += d.getHours()+':' if (d.getMinutes()< 10) tmp += '0' tmp += d.getMinutes()+':' if (d.getSeconds() < 10) tmp += '0' tmp += d.getSeconds()+" GMT" return tmp } // An Alternate "GMT" Function: NOTE! This--changed for hyphens and no (19) function altGMT(d) { tmp = day(d.getDay())+', '+d.getDate()+'-'+month(d.getMonth()) tmp += '-'+d.getYear()+' ' if (d.getHours() < 10) tmp += '0' tmp += d.getHours()+':' if (d.getMinutes()< 10) tmp += '0' tmp += d.getMinutes()+':' if (d.getSeconds() < 10) tmp += '0' tmp += d.getSeconds() return tmp } // --------------------COOKIE DATA UTILITIES--------------------- // Cookie Flattener function flatten() { var tmp = ""+this.name+"="+this.value tmp += (this.expires == null) ? "" : "; expires="+this.expires tmp += (this.path == null) ? "" : "; path="+this.path tmp += (this.domain == null) ? "" : "; domain="+this.domain tmp += (this.isSecure) ? "; secure" : "" return tmp } // Store Cookie in the jar. This is counter-intuitive but it works. You // use assignment, not addition. Sigh. function store() {document.cookie = this.flatten()} // Set Name function setName(aName){this.name = encookie(aName)} // Set Value function setValue(aValue){this.value = encookie(aValue)} // Get Name function getName(aName){this.name = decookie(aName)} // Get Value function getValue(aValue){this.value = decookie(aValue)} // Check for WIN16 -- avoid the date bug function isWin16() { return (navigator.userAgent.toUpperCase().indexOf('WIN16') != -1) } // Expiration Times function setExpires(howLong) { var now = new Date() var epoch = isWin16() ? 0 : new Date(0) var msecs = now.getTime() var offset = now.getTimezoneOffset() * 60000 var t = 0 howLong = howLong.toLowerCase() // determine how long until expiration if (howLong == 'now') t = msecs - (3600000 * 24 * 365) // overkill if (howLong == 'hour') t = msecs + (3600000) // 60 minutes if (howLong == 'day') t = msecs + (3600000 * 24) // 24 hours if (howLong == 'week') t = msecs + (3600000 * 24 * 7) // 7 days if (howLong == 'month') t = msecs + (3600000 * 24 * 30) // 30 days if (howLong == 'year') t = msecs + (3600000 * 24 * 365) // 365 days // set the expiration exp = isWin16() ? (t - offset) : ((t - epoch.getTime()) - offset) now.setTime(exp) this.expires = myGMT(now) return this } // Turn on security function secure(){this.isSecure = true} // Turn off security function unsecure(){this.isSecure = false} // get rid of a cookie right now function crumble() { this.setExpires('now') this.store() } // revive a crumbled cookie function uncrumble() { this.expires = null this.store() } // Cookie Constructor function JSCCookie(cname, cvalue) { // Create and initialize cookie slots this.name = encookie(cname) // readable and writeable this.value = encookie(cvalue) // readable and writeable this.expires = null // writeable this.isSecure = false // writeable this.path = null // writeable this.domain = null // writeable // Set cookie methods this.setExpires = setExpires this.crumble = crumble this.uncrumble = uncrumble this.flatten = flatten this.store = store this.secure = secure this.unsecure = unsecure this.setName = setName this.getName = getName this.setValue = setValue this.getValue = getValue } // --------------------COOKIE JAR UTILITIES--------------------- // find a cookie by partial string function findCookie(name) { num_in_jar = Math.min(document.cookie.length, count(document.cookie, ';')+1) if (num_in_jar != 0) { var tmp = ""+document.cookie for (var i = 0; i < num_in_jar; i++) { var crumb = stripBlanks(chopit(tmp, ";")) tmp = lopit(tmp, ";") var cname = decookie(chopit(crumb, "=")) if (cname.indexOf(name) >= 0) return crumb } } return null } // Fetch a value (by partial string) function fetch(name) { var crumb = findCookie(name) if (crumb == null) return null var cname = decookie(chopit(crumb, "=")) var cvalue = decookie(lopit(crumb, "=")) if (cname.indexOf(name) >= 0) return cvalue return null } // Fetch a name (by partial string) function fetchName(name) { var crumb = findCookie(name) if (crumb == null) return null var cname = decookie(chopit(crumb, "=")) var cvalue = decookie(lopit(crumb, "=")) if (cname.indexOf(name) >= 0) return cname return null } // Fetch a particular cookie (by partial string) function fetchCookie(name) { var crumb = findCookie(name) if (crumb == null) return null var cname = decookie(chopit(crumb, "=")) var cvalue = decookie(lopit(crumb, "=")) if (cname.indexOf(name) >= 0) { var c = new JSCCookie(cname, cvalue) return c } return null } // --------------------COOKIE INFO UTILITIES--------------------- // Cookie Information function cookiePeek() { size_in_jar = document.cookie.length num_in_jar = Math.min(document.cookie.length, count(document.cookie, ';')+1) if (num_in_jar != 0) { document.write("<b>Current Cookies:</b> "+ document.cookie+"<p><blockquote>") var tmp = ""+document.cookie var crumb, cname, cvalue for (var i = 0; i < num_in_jar; i++) { crumb = chopit(tmp, ";") tmp = lopit(tmp, ";") cname = decookie(chopit(crumb, "=")) cvalue = decookie(lopit(crumb, "=")) document.write("<b>Name:</b> <FONT COLOR='770000'>"+cname+"</FONT> ") document.write("<b>Value:</b> <FONT COLOR='770000'>"+cvalue+"<br></FONT>") } document.write("</blockquote>") } else document.write("<b>The Cookie Jar is <FONT COLOR='770000'>Empty</FONT>.</b><br>") document.write('There are a total of '+num_in_jar+' cookies taking up '+ size_in_jar+' bytes<p>') } // --------------------FORMAT UTILITIES--------------------- function comment(aString) { document.write("<FONT SIZE=4 COLOR='007777'><b>") document.write(aString) document.write("</b></FONT><br>") } function observe(aString) { document.write("<FONT SIZE=3 COLOR='007777'>") document.write(aString) document.write("</FONT><BR>") } function pre() { var l = pre.arguments.length document.write("<PRE><FONT SIZE=3 COLOR='770000'>") for (var i = 0; i < l; i++) { document.writeln(pre.arguments[i]) } document.write("</FONT></PRE>") } <!-- done hiding --></SCRIPT></HEAD> <BODY BGCOLOR="FFFFFF"> <FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50 ALIGN = LEFT>The JavaScript Cookbook Cookie Routines</H1></FONT> <BLOCKQUOTE><FONT COLOR="770000">Use these routines to set and retrieve persistent data from the user's site. Cookies are small pieces of data that can be stored between visits to your page. Use cookies to:<ul> <li> Save biographical information to reduce typing <li> Show new information that dates after the last visit. <li> Store reminders and to-do information <li> Remember user preferences <li> ... and much more </ul> These examples progress through an entire series of transactions which use the JavaScript Cookbook Cookie library. The library calls are shown for each example. These are not standard Cookie calls, but ones that have been developed especially for this CD-ROM and are now made available to you. These routines make cookie-baking a pleasure rather than a chore.<p> These examples use the "Acme" sample data from the official Netscape <a href="http://www.netscape.com/newsref/std/cookie_spec.html" target="JCLangRef">HTTP Cookies</A> reference sheet. This helps the reader orient between these JavaScript Cookbook routines and the built-in Netscape JavaScript routines. <font size=2>(This link may change without prior notice).</font><p> An important note. In early versions of Netscape Navigator, cookies will expire immediately unless their path is set to "/". </BLOCKQUOTE> </FONT> <BR><BR> <SCRIPT> comment("Start with a (probably) empty jar") pre( 'cookiePeek()') cookiePeek() comment("Create a new Customer") pre( 'var myCookie = new JSCCookie("CUSTOMER", "Wile E. Coyote")', 'myCookie.store()', 'cookiePeek()') var myCookie = new JSCCookie("CUSTOMER", "Wile E. Coyote") myCookie.store() cookiePeek() comment("Create a Part Number") pre( 'var newCookie = new JSCCookie("Part_Number", "Rocket Launcher 0001")', 'newCookie.store()', 'cookiePeek()') var newCookie = new JSCCookie("Part_Number", "Rocket Launcher 0001") newCookie.store() cookiePeek() comment("Crumble the customer") pre('myCookie.crumble()', 'cookiePeek()') myCookie.crumble() cookiePeek() comment("Secure the launcher and peek") pre('newCookie.secure()','newCookie.store()', 'cookiePeek()') newCookie.secure() newCookie.store() cookiePeek() comment("Unsecure the launcher and peek") pre('newCookie.unsecure()','newCookie.store()', 'cookiePeek()') newCookie.unsecure() newCookie.store() cookiePeek() comment("Crumble the launcher") pre('newCookie.crumble()', 'cookiePeek()') newCookie.crumble() cookiePeek() comment("'Uncrumble' the cookies by setting expires to null") pre( 'newCookie.uncrumble()', 'myCookie.uncrumble()', 'cookiePeek()') newCookie.uncrumble() myCookie.uncrumble() cookiePeek() comment("Set the Customer domain to .acme.com") pre( 'myCookie.domain = \'.acme.com\'', 'myCookie.store()', 'cookiePeek()') myCookie.domain = '.acme.com' myCookie.store() cookiePeek() comment("Reset the domain to null") pre( 'myCookie.domain = null', 'myCookie.store()', 'cookiePeek()') myCookie.domain = null myCookie.store() cookiePeek() comment("Set the Customer path to /foo") pre( 'myCookie.path = \'/foo\'', 'myCookie.store()', 'cookiePeek()') myCookie.path = '/foo' myCookie.store() cookiePeek() comment("Reset the domain to null") pre( 'myCookie.path = null', 'myCookie.store()', 'cookiePeek()') myCookie.path = null myCookie.store() cookiePeek() comment("Fetch the customer") pre( 'var tmp = fetchCookie(\'CUSTOM\')', 'observe(\'Fetched: \'+tmp.flatten())', 'observe(\'Fetched: \'+fetch(\'CUSTOM\'))') var tmp = fetchCookie('CUSTOM') observe('Fetched: '+tmp.flatten()) observe('Fetched: '+fetch('CUSTOM')) document.write("<P>") comment("Crumble the two cookies") pre( 'newCookie.crumble()', 'myCookie.crumble()', 'cookiePeek()') newCookie.crumble() myCookie.crumble() cookiePeek() </SCRIPT> <h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5> </BODY></HTML>